clear all;

sig=2.0;
bita=0.954;

colr='k:'
colr_pp='g--'
colr_cov='r-'
colr_reprof='b-.'

jz=3;
ja=12;

amin=0;
amax=0.08;

q0=0.961538
delta=0.8775
freq=1.0
averz=0.5
averz=averz*freq
debtfactor=1.0/((1.0-q0*delta)*freq);
amax=(amax/averz)*debtfactor;

% .. benchmark data ..
dirpath = ['welfare\welf_def000.dat'];
mydata = importdata(dirpath, ' ', 1);
AA=mydata.data;
% filtering
row_idx=(AA(:,2)==jz); 
A_filt=AA(row_idx,:); % filtering
zz= A_filt(:,4);
aa= A_filt(:,3);
%aa=aa/.5;
aa=(aa/averz)*debtfactor;
val= A_filt(:,5);
valraw=((1-bita)*(1-sig)*val).^(1/(1-sig));
val=((1-sig)*val).^(1/(1-sig));


%val=(val./val)-1;

% .. nomktaccess - pari passu ..
dirpath = ['counterfact\mktaccess\welfare\welf_def000.dat'];
mydata = importdata(dirpath, ' ', 1);
AA=mydata.data;
% filtering
row_idx=(AA(:,2)==jz); 
A_filt=AA(row_idx,:); % filtering
aa_pp= A_filt(:,3);
%aa_pp=aa_pp/.5;
aa_pp=(aa_pp/averz)*debtfactor;
val_pp= A_filt(:,5);
val_pp=((1-sig)*val_pp).^(1/(1-sig));
val_pp=(val_pp./val)-1;

% .. covenant ..
dirpath = ['counterfact\covenant\welfare\welf_def000.dat'];
mydata = importdata(dirpath, ' ', 1);
AA=mydata.data;
% filtering
row_idx=(AA(:,2)==jz); 
A_filt=AA(row_idx,:); % filtering
aa_cov= A_filt(:,3);
%aa_cov=aa_cov/.5;
aa_cov=(aa_cov/averz)*debtfactor;
val_cov= A_filt(:,5);
val_cov=((1-sig)*val_cov).^(1/(1-sig));
val_cov=(val_cov./val)-1;

% .. reprofiling ..
dirpath = ['counterfact\reprof\kap060\welfare\welf_def000.dat'];
mydata = importdata(dirpath, ' ', 1);
AA=mydata.data;
% filtering
row_idx=(AA(:,2)==jz); 
A_filt=AA(row_idx,:); % filtering
aa_reprof= A_filt(:,3);
%aa_reprof=aa_reprof/.5;
aa_reprof=(aa_reprof/averz)*debtfactor;
val_reprof= A_filt(:,5);
val_reprof=((1-sig)*val_reprof).^(1/(1-sig));
val_reprof=(val_reprof./val)-1;
val=(val./val)-1;



figure(4);
plot(aa_pp,val_pp*100,colr_pp,'LineWidth',2);
hold on;
plot(aa_cov,val_cov*100,colr_cov,'LineWidth',2);
hold on;
plot(aa_reprof,val_reprof*100,colr_reprof,'LineWidth',2); 
text(aa(3), -.1,'Debt Relief','Interpreter','latex' ,'FontSize',12,'Color','b')
text(aa(5), -.1,'Pari Passu','Interpreter','latex' ,'FontSize',12,'Color','g')
text(aa(10), -.1,'No-Dilution','Interpreter','latex' ,'FontSize',12,'Color','r')
ylabel('Welfare rel. baseline (\% CE)','Interpreter','latex' ,'FontSize',12);
xlabel('Debt','Interpreter','latex' ,'FontSize',12)
ylim([-.5 .5]);
xlim([amin amax]);
grid on;


figure(2)
plot(aa,valraw./valraw(1))
% REPORT 
pc_cov=val_cov %(val_cov./val)-1
pc_reprof=val_reprof %(val_reprof./val)-1
pc_pp=val_pp %(val_pp./val)-1

% Create a table with the data and variable names

%T = table(aa(ja), pc_cov(ja,1), pc_reprof(ja,1), pc_pp(ja,1), 'VariableNames', { 'a', 'cov', 'reprof', 'pp'} )
T = table(zz(1:ja), aa(1:ja), pc_cov(1:ja,1), pc_reprof(1:ja,1), pc_pp(1:ja,1), 'VariableNames', {'z', 'a', 'cov', 'reprof', 'pp'} )

% Write data to text file
%writetable(T, 'slides_dyn\flex_epsshock\amplification.txt')
